<%@ include file="/html/common/init.jsp" %>
<%@ page import="com.liferay.portal.kernel.portlet.LiferayWindowState" %>
<%@ page import="com.liferay.portal.kernel.language.UnicodeLanguageUtil" %>
<%@ page import="com.liferay.portlet.shopping.model.ShoppingCategory"%>
<%@ page import="com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil"%>


<script type="text/javascript" src="/js/jquery.js"></script>

<%
String path_S = getServletContext().getRealPath("/");

//List defineField = null;
String redirect = ParamUtil.getString(request, "redirect");

List<CustomField> defineFieldList = (List) request.getAttribute("DEFINE_FIELD_LIST");

ShoppingItem item = (ShoppingItem)request.getAttribute("SHOPPING_ITEM");
long itemId = BeanParamUtil.getLong(item, request, "itemId");

long categoryId = BeanParamUtil.getLong(item, request, "categoryId", ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);

// Fields

ShoppingItemField[] itemFields = null;

int fieldsCount = ParamUtil.getInteger(request, "fieldsCount");


String fieldsCountParam = request.getParameter("fieldsCount");


if ((fieldsCountParam == null) || (fieldsCountParam.equals(StringPool.NULL))) {
	if (item != null) {
		itemFields = (ShoppingItemField[])ShoppingItemFieldLocalServiceUtil.getItemFields(itemId).toArray(new ShoppingItemField[0]);
		fieldsCount = itemFields.length;
	}
	else {
		itemFields = new ShoppingItemField[0];
	}
}
else {
	itemFields = new ShoppingItemField[fieldsCount];
}

int fieldId = ParamUtil.getInteger(request, "fieldId", -1);

String fieldsQuantities = "";

if (item != null) {
	fieldsQuantities = GetterUtil.getString(item.getFieldsQuantities());
}

// Prices

ShoppingItemPrice[] itemPrices = null;

int pricesCount = ParamUtil.getInteger(request, "pricesCount", 1);
String pricesCountParam = request.getParameter("pricesCount");
if ((pricesCountParam == null) || (pricesCountParam.equals(StringPool.NULL))) {
	if (item != null) {
		itemPrices = (ShoppingItemPrice[])ShoppingItemPriceLocalServiceUtil.getItemPrices(itemId).toArray(new ShoppingItemPrice[0]);
		pricesCount = itemPrices.length;
	}
	else {
		itemPrices = new ShoppingItemPrice[1];
	}
}
else {
	itemPrices = new ShoppingItemPrice[pricesCount];
}

int priceId = ParamUtil.getInteger(request, "priceId", -1);
PortletURL renderURL = renderResponse.createRenderURL();
//renderURL.setParameter(ActionRequest.ACTION_NAME, "addProcess");
renderURL.setParameter(Constants.CMD, "defineField");
%>

<portlet:renderURL var="backURL">
     <portlet:param name="jspPage" value="/html/item/item_list.jsp" />
	 </portlet:renderURL>

 <portlet:actionURL name="addProcess" var="editItemURL">
<portlet:param name="<%= Constants.CMD %>" value="<%= Constants.ADD %>" />
</portlet:actionURL> 

<aui:form action="<%= editItemURL %>" enctype="multipart/form-data" method="post" name="fm" onSubmit='<%= "event.preventDefault(); " + renderResponse.getNamespace() + "saveItem();" %>'>
	<input name="scroll" type="hidden" value="" />
	<aui:input name="<%= Constants.CMD %>" type="hidden" />
	<aui:input name="redirect" type="hidden" value="<%= redirect %>" />
	<aui:input name="categoryId" type="hidden" value="<%= categoryId %>" />
	<aui:input name="itemId" type="hidden" value="<%= itemId %>" />
	<aui:input name="fieldsCount" type="hidden" value="<%= fieldsCount %>" />
	<aui:input name="fieldId" type="hidden" />
	<aui:input name='<%= "fieldName" + fieldsCount %>' type="hidden" />
	<aui:input name='<%= "fieldValues" + fieldsCount %>' type="hidden" />
	<aui:input name='<%= "fieldDescription" + fieldsCount %>' type="hidden" />
	<aui:input name="fieldsQuantities" type="hidden" value="<%= fieldsQuantities %>" />
	<aui:input name="pricesCount" type="hidden" value="<%= pricesCount %>" />
	<aui:input name="priceId" type="hidden" />
	
	<liferay-ui:header
		backURL="<%= redirect %>"
		title="item"
	/>
	<a href="<%= backURL.toString() %>" style="float:right">&laquo;back</a>

	<liferay-ui:error exception="<%= DuplicateItemSKUException.class %>" message="the-item-sku-you-requested-is-already-taken" />
	<liferay-ui:error exception="<%= ItemNameException.class %>" message="please-enter-a-valid-name" />
	<liferay-ui:error exception="<%= ItemSKUException.class %>" message="please-enter-a-valid-item-sku" />

	
	<aui:fieldset>
		
			<aui:field-wrapper label="category">

				<%
				String categoryName = "";

				if (categoryId != ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
					ShoppingCategory category = ShoppingCategoryLocalServiceUtil.getCategory(categoryId);

					category = category.toEscapedModel();

					categoryName = category.getName();
				}
				%>

				<portlet:renderURL var="viewCategoryURL">
					<portlet:param name="struts_action" value="/category/category_list" />
					<portlet:param name="categoryId" value="<%= String.valueOf(categoryId) %>" />
				</portlet:renderURL>

				<aui:a href="<%= viewCategoryURL %>" id="categoryName" label="<%= categoryName %>" />

				<portlet:renderURL var="selectCategoryURL" windowState="<%= LiferayWindowState.POP_UP.toString() %>">
					<portlet:param name="jspPage" value="/html/item/select_category.jsp" />
					<portlet:param name="categoryId" value="0" />
				</portlet:renderURL>

				<%
				String taglibOpenCategoryWindow = "var categoryWindow = window.open('" + selectCategoryURL + "', 'category', 'directories=no,height=640,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width=680'); void(''); categoryWindow.focus();";
				%>
<!--  <h6>qqq <%= categoryId %></h6>				
<h6>qqq <%= categoryName %></h6>-->
				<aui:button onClick="<%= taglibOpenCategoryWindow %>" type="button" value="select" />

				<aui:button onClick='<%= renderResponse.getNamespace() + "removeCategory();" %>' type="button" value="remove" />
			</aui:field-wrapper>
		

		<aui:input bean="<%= item %>" model="<%= ShoppingItem.class %>" name="sku" />

		<aui:input bean="<%= item %>" model="<%= ShoppingItem.class %>" name="name" />

		<aui:input bean="<%= item %>" model="<%= ShoppingItem.class %>" name="description" />

		<aui:input bean="<%= item %>" model="<%= ShoppingItem.class %>" name="properties" />
		
		<%@ include file="/html/item/dynamic_field.jsp" %>
		
		
		<aui:input bean="<%= item %>" model="<%= ShoppingItem.class %>" name="requiresShipping" />

		<aui:input bean="<%= item %>" model="<%= ShoppingItem.class %>" name="featured" />

		<c:if test="<%= fieldsCount == 0 %>">
			<aui:input bean="<%= item %>" model="<%= ShoppingItem.class %>" name="stockQuantity" />
		</c:if>

	<!-- 	<c:if test="<%= item == null %>">
			<aui:field-wrapper label="permissions">
				<liferay-ui:input-permissions
					modelName="<%= ShoppingItem.class.getName() %>"
				/>
			</aui:field-wrapper>
		</c:if>    -->
	</aui:fieldset>

	

	<liferay-ui:panel-container extended="<%= true %>" persistState="<%= true %>">
		

		<liferay-ui:panel collapsible="<%= true %>" extended="<%= true %>" persistState="<%= true %>" title='<%= LanguageUtil.get(pageContext, "prices") %>'>
			<aui:input name="prices" type="hidden" />

			<aui:fieldset>
				<table class="lfr-table">

				<%
				for (int i = 0; i < pricesCount; i++) {
					int curPriceId = i;

					if (priceId > -1 && i >= priceId) {
						curPriceId++;
					}

					int minQuantity = ParamUtil.getInteger(request, "minQuantity" + curPriceId, 0);
					String minQuantityParam = request.getParameter("minQuantity" + curPriceId);
					if ((minQuantityParam == null) || (minQuantityParam.equals(StringPool.NULL))) {
						if (itemPrices[curPriceId] != null) {
							minQuantity = itemPrices[curPriceId].getMinQuantity();
						}
					}

					int maxQuantity = ParamUtil.getInteger(request, "maxQuantity" + curPriceId);
					String maxQuantityParam = request.getParameter("maxQuantity" + curPriceId);
					if ((maxQuantityParam == null) || (maxQuantityParam.equals(StringPool.NULL))) {
						if (itemPrices[curPriceId] != null) {
							maxQuantity = itemPrices[curPriceId].getMaxQuantity();
						}
					}

					double price = ParamUtil.getDouble(request, "price" + curPriceId);
					String priceParam = request.getParameter("price" + curPriceId);
					if ((priceParam == null) || (priceParam.equals(StringPool.NULL))) {
						if (itemPrices[curPriceId] != null) {
							price = itemPrices[curPriceId].getPrice();
						}
					}

					double discount = ParamUtil.getDouble(request, "discount" + curPriceId) / 100;
					String discountParam = request.getParameter("discount" + curPriceId);
					if ((discountParam == null) || (discountParam.equals(StringPool.NULL))) {
						if (itemPrices[curPriceId] != null) {
							discount = itemPrices[curPriceId].getDiscount();
						}
					}

					boolean taxable = ParamUtil.getBoolean(request, "taxable" + curPriceId, true);
					String taxableParam = request.getParameter("taxable" + curPriceId);
					if ((taxableParam == null) || (taxableParam.equals(StringPool.NULL))) {
						if (itemPrices[curPriceId] != null) {
							taxable = itemPrices[curPriceId].isTaxable();
						}
					}

					double shipping = ParamUtil.getDouble(request, "shipping" + curPriceId);
					String shippingParam = request.getParameter("shipping" + curPriceId);
					if ((shippingParam == null) || (shippingParam.equals(StringPool.NULL))) {
						if (itemPrices[curPriceId] != null) {
							shipping = itemPrices[curPriceId].getShipping();
						}
					}

					boolean useShippingFormula = ParamUtil.getBoolean(request, "useShippingFormula" + curPriceId, true);
					String useShippingFormulaParam = request.getParameter("useShippingFormula" + curPriceId);
					if ((useShippingFormulaParam == null) || (useShippingFormulaParam.equals(StringPool.NULL))) {
						if (itemPrices[curPriceId] != null) {
							useShippingFormula = itemPrices[curPriceId].isUseShippingFormula();
						}
					}

					boolean active = ParamUtil.getBoolean(request, "active" + curPriceId, true);
					String activeParam = request.getParameter("active" + curPriceId);
					if ((activeParam == null) || (activeParam.equals(StringPool.NULL))) {
						if (itemPrices[curPriceId] != null) {
							int status = itemPrices[curPriceId].getStatus();

							if (status == ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT || status == ShoppingItemPriceConstants.STATUS_ACTIVE) {
								active = true;
							}
							else {
								active = false;
							}
						}
					}

					String defaultPriceParam = request.getParameter("defaultPrice");
					boolean defaultPrice = (curPriceId == 0 ? true : false);
					if (Validator.isNotNull(defaultPriceParam)) {
						if (ParamUtil.getInteger(request, "defaultPrice") == curPriceId) {
							defaultPrice = true;
						}
						else {
							defaultPrice = false;
						}
					}
					else {
						if (itemPrices[curPriceId] != null) {
							int status = itemPrices[curPriceId].getStatus();

							if (status == ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT) {
								defaultPrice = true;
							}
							else {
								defaultPrice = false;
							}
						}
					}
				%>

					<tr>
						<td>
							<table class="lfr-table">
							<tr>
								<td>
									<aui:input field="minQuantity" fieldParam='<%= "minQuantity" + i %>' label="min-qty" model="<%= ShoppingItemPrice.class %>" name="minQuantity" value="<%= String.valueOf(minQuantity) %>" />
								</td>
								<td>
									<aui:input field="maxQuantity" fieldParam='<%= "maxQuantity" + i %>' label="max-qty" model="<%= ShoppingItemPrice.class %>" name="maxQuantity" value="<%= String.valueOf(maxQuantity) %>" />
								</td>
								<td>
									<aui:input field="price" fieldParam='<%= "price" + i %>' format="<%= doubleFormat %>" label="price" model="<%= ShoppingItemPrice.class %>" name="price" value="<%= String.valueOf(price) %>" />
								</td>
								<td>
									<aui:input field="discount" fieldParam='<%= "discount" + i %>' label="discount" model="<%= ShoppingItemPrice.class %>" name="discount" value="<%= percentFormat.format(discount) %>" />
								</td>
								
								<td>
									<aui:input label="taxable" name='<%= "taxable" + i %>' param='<%= "taxable" + i %>' type="checkbox" value="<%= taxable %>" />
								</td>
							</tr>
							</table>

							<table class="lfr-table">
							<tr>
							    <td>
									<aui:input field="shipping" fieldParam='<%= "shipping" + i %>' format="<%= doubleFormat %>" model="<%= ShoppingItemPrice.class %>" name="shipping" value="<%= String.valueOf(shipping) %>" />
								</td>
								
								<td>
									<aui:input label="use-shipping-formula" name='<%= "useShippingFormula" + i %>' type="checkbox" value="<%= useShippingFormula %>" />
								</td>
								<td>
									<aui:input label="active" name='<%= "active" + i %>' type="checkbox" value="<%= active %>" />
								</td>
								<td>
									<aui:input checked="<%= defaultPrice %>" label="default" name="defaultPrice" onClick='<%= "document." + renderResponse.getNamespace() + "fm." + renderResponse.getNamespace() + "active" + i + ".checked = true;" %>' type="radio" value="<%=i %>" />
								</td>

								<c:if test="<%= pricesCount > 1 %>">
									<td>
										<aui:button onClick='<%= renderResponse.getNamespace() + "deletePrice(" + i + ");" %>' type="button" value="delete" />
									</td>
								</c:if>
							</tr>
							</table>

							<c:if test="<%= (i + 1) < pricesCount %>">
								<br />
							</c:if>
						</td>
					</tr>

				<%
				}
				%>

				</table>
			</aui:fieldset>

			<!-- <aui:button onClick='<%= renderResponse.getNamespace() + "addPrice();" %>' type="button" value="add-price" /> -->
		</liferay-ui:panel>

		<liferay-ui:panel collapsible="<%= true %>" extended="<%= true %>" persistState="<%= true %>" title='<%= LanguageUtil.get(pageContext, "images") %>'>
					



			<aui:fieldset>
				<aui:input label="small-image-url" name="smallImageURL" />

				<span style="font-size: xx-small;">-- <%= LanguageUtil.get(pageContext, "or").toUpperCase() %> --</span> <liferay-ui:message key="small-image" />

				<aui:input label="" name="smallFile" type="file" />

				<aui:input label="use-small-image" name="smallImage" type="checkbox" checked="true"/>

				<aui:input label="medium-image-url" name="mediumImageURL" />

				<span style="font-size: xx-small;">-- <%= LanguageUtil.get(pageContext, "or").toUpperCase() %> --</span> <liferay-ui:message key="medium-image" />

				<aui:input label="" name="mediumFile" type="file" />

				<aui:input label="use-medium-image" name="mediumImage" type="checkbox" checked="true"/>

				<aui:input label="large-image-url" name="largeImageURL" />

				<span style="font-size: xx-small;">-- <%= LanguageUtil.get(pageContext, "or").toUpperCase() %> --</span> <liferay-ui:message key="large-image" />

				<aui:input label="" name="largeFile" type="file" />

				<aui:input label="use-large-image" name="largeImage" type="checkbox" checked="true"/>
			</aui:fieldset>
		</liferay-ui:panel>
	</liferay-ui:panel-container>
	
	<aui:button-row>
		<aui:button type="submit" />

		<input type="button" value="Cancel" onclick="location.href='<%=backURL.toString()%>'"/>
		
	</aui:button-row>
</aui:form>

<aui:script>
	function <portlet:namespace />addField() {
		document.<portlet:namespace />fm.scroll.value = "<portlet:namespace />fields";
		document.<portlet:namespace />fm.<portlet:namespace />fieldsCount.value = <%= fieldsCount + 1 %>;
		submitForm(document.<portlet:namespace />fm);
	}

	function <portlet:namespace />addPrice() {
		document.<portlet:namespace />fm.scroll.value = "<portlet:namespace />prices";
		document.<portlet:namespace />fm.<portlet:namespace />pricesCount.value = <%= pricesCount + 1 %>;
		submitForm(document.<portlet:namespace />fm);
	}

	function <portlet:namespace />deleteField(i) {
		document.<portlet:namespace />fm.scroll.value = "<portlet:namespace />fields";
		document.<portlet:namespace />fm.<portlet:namespace />fieldsCount.value = <%= fieldsCount - 1 %>;
		document.<portlet:namespace />fm.<portlet:namespace />fieldId.value = i;
		submitForm(document.<portlet:namespace />fm);
	}

	function <portlet:namespace />deletePrice(i) {
		if (document.<portlet:namespace />fm.<portlet:namespace />defaultPrice[i].checked) {
			alert("<%= UnicodeLanguageUtil.get(pageContext, "you-cannot-delete-or-deactivate-a-default-price") %>");
		}
		else if (document.<portlet:namespace />fm.<portlet:namespace />pricesCount.value > 1) {
			document.<portlet:namespace />fm.scroll.value = "<portlet:namespace />prices";
			document.<portlet:namespace />fm.<portlet:namespace />pricesCount.value = <%= pricesCount - 1 %>;
			document.<portlet:namespace />fm.<portlet:namespace />priceId.value = i;
			submitForm(document.<portlet:namespace />fm);
		}
	}

	function <portlet:namespace />editItemQuantities() {
		var itemQuantitiesURL = "<liferay-portlet:renderURL windowState="<%= LiferayWindowState.POP_UP.toString() %>" anchor="false"><portlet:param name="struts_action" value="/shopping/edit_item_quantities" /></liferay-portlet:renderURL>&<portlet:namespace />fieldsQuantities=" + document.<portlet:namespace />fm.<portlet:namespace />fieldsQuantities.value;

		<%
		for (int i = 0; i < fieldsCount; i++) {
		%>

			itemQuantitiesURL += "&<portlet:namespace />n<%= i %>=" + encodeURIComponent(document.<portlet:namespace />fm.<portlet:namespace />fieldName<%= i %>.value);
			itemQuantitiesURL += "&<portlet:namespace />v<%= i %>=" + encodeURIComponent(document.<portlet:namespace />fm.<portlet:namespace />fieldValues<%= i %>.value);

		<%
		}
		%>

		var itemQuantitiesWindow = window.open(itemQuantitiesURL, "itemQuantities", "directories=no,height=400,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,width=300");

		void("");

		itemQuantitiesWindow.focus();
	}

	function <portlet:namespace />removeCategory() {
		
		document.<portlet:namespace />fm.<portlet:namespace />categoryId.value = "<%= ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID %>";
		removeDefineField();
		var nameEl = document.getElementById("<portlet:namespace />categoryName");

		nameEl.href = "";
		nameEl.innerHTML = "";
	}

	function <portlet:namespace />saveItem() {
		document.<portlet:namespace />fm.<portlet:namespace /><%= Constants.CMD %>.value = "<%= (item == null) ? Constants.ADD : Constants.UPDATE %>";
		submitForm(document.<portlet:namespace />fm);
	}

	function <portlet:namespace />selectCategory(categoryId, categoryName) {
		document.<portlet:namespace />fm.<portlet:namespace />categoryId.value = categoryId;

		callDefineField(categoryId);
		var nameEl = document.getElementById("<portlet:namespace />categoryName");

		nameEl.href = "<portlet:renderURL><portlet:param name="struts_action" value="/shopping/view" /></portlet:renderURL>&<portlet:namespace />categoryId=" + categoryId;
		nameEl.innerHTML = categoryName + "&nbsp;";
	}

	function refreshCartPortlet(categoryId) {
		var plid = themeDisplay.getPlid();
		var nsShort = ("<portlet:namespace/>".substr(1)).substr(0, "<portlet:namespace/>".length-2);
		var portletId = "#p_p_id<portlet:namespace/>";
		var loadId = "p_load<portlet:namespace/>";
		url = "/c/portal/render_portlet?p_l_id=" + plid+ "&p_p_id="+nsShort+"&p_p_action=0&p_p_state=normal&p_p_mode=view&cmd=defineField&categoryId="+categoryId;
		jQuery(portletId).load(url);
	}

	function removeDefineField() {
		var url = '<%= renderURL.toString() %>&categoryId=0';
		jQuery.ajax({ url: url, success: function() {
			refreshCartPortlet("0");
	    }});  
	}
	
	function callDefineField(categoryId) {	
		var url = '<%= renderURL.toString() %>&categoryId='+categoryId;
		jQuery.ajax({ url: url, success: function() {
			refreshCartPortlet(categoryId);
	    }});       
	} 

</aui:script>